<?php
namespace app\shouyin\admin;
use app\system\admin\Admin;
use app\shouyin\model\Course as CourseModel;
use app\product\model\Project as ProjectModel;
use app\product\model\Product as ProductModel;
use think\Db;
class Course extends Admin
{
    protected $hisiModel = 'Course';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize()
    {
        parent::initialize();
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $projects = ProjectModel::getSelect(ProjectModel::all());
            $this->assign('projects',$projects);
            $products = ProductModel::getSelect(ProductModel::all());
            $this->assign('products',$products);
        }
    }

    /**
     * 添加
     */
    public function add1()
    {
        $where = [];
        $db = new CourseModel;
        $pk = $db->getPk();
        $id = $this->request->param($pk);

        $where[]= [$pk, '=', $id];
        $where  = $this->getRightWhere($where);
        if($this->request->isAjax()){
            $postData = $this->request->post();
            if($postData['type'] == 1){
                $postData['course_json'] = json_encode([
                    'price1' => $postData['price1'],
                    'price2' => $postData['price2']
                ]);
            }else if($postData['type'] == 2){
                $postData['course_json'] = json_encode($postData['project']);
            }else if($postData['type'] == 3){
                $postData['course_json'] = json_encode($postData['product']);
            }

            if(!$db->save($postData)){
                return $this->error('新增出错，请重试~');
            }
            $this->success('保存成功','');
        }

        $formData = $model->where($where)->find();

        if (!$formData) {
            return $this->error('数据不存在或没有权限');
        }

        $this->assign('formData', $formData);

        $template = $this->request->param('template', 'form');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $name      = $this->request->param('name', '');
            if(!empty($name)){
                $where[] = ['name','like','%'.$name.'%'];
            }
            $data['data'] = CourseModel::where($where)
                            ->field('id,name,course_json,course_json as course_json_text,price,sort,createtime,updatetime,status,type,type as type_text')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $data['count'] = CourseModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);
        }
        return $this->fetch();
    }

    public function import()
    {
        $where      = [];
        $name      = $this->request->param('name', '');
        if(!empty($name)){
            $where[] = ['name','like','%'.$name.'%'];
        }
        $data['data'] = CourseModel::where($where)
                        ->field('id,name,course_json,course_json as course_json_text,price,sort,createtime,updatetime,status,type,type as type_text')
                        ->order('id desc')
                        ->select();
        $data['count'] = CourseModel::where($where)->count('id');
        $data['code'] = 0;
        return json($data);
    }
}
